package com.yict.csms.system.dao.impl;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import com.yict.common.dao.impl.BaseDao;
import com.yict.csms.system.dao.ICostDao;
import com.yict.csms.system.dao.IDataDictDao;
import com.yict.csms.system.dao.IProfessionDao;
import com.yict.csms.system.entity.BudgetNum;
import com.yict.csms.system.entity.Cost;
import com.yict.csms.system.entity.DataDict;
import com.yict.csms.system.entity.Profession;
import com.yict.csms.system.util.DictionaryUtils;

@Repository("costDao")
public class CostDaoImpl extends BaseDao<Cost, Long> implements ICostDao {
	private IProfessionDao professionDao;
	private IDataDictDao dataDictDao;

	@Override
	public boolean check(Date beginDate, Date endDate, DataDict dataDict, DataDict currency) {
		boolean flag = true;
		StringBuilder queryString = new StringBuilder("from Cost t where 1=1 and t.servicedate >= :beginDate and t.servicedate <= :endDate and t.status =:dataDict");
		if(currency != null) {
			queryString.append(" and t.ratecurrency =:ratecurrency");
		}
		try {
			Query query = this.getSession().createQuery(queryString.toString());
			query.setParameter("beginDate", beginDate);
			query.setParameter("endDate", endDate);
			query.setParameter("dataDict", dataDict.getDictid());
			if(currency != null) {
				query.setParameter("ratecurrency", currency.getDictid());
			}
			List<String> list = query.list();
			if(list == null || list.size() == 0) {
				flag = false;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	public BigDecimal sumByMonth(final Long companyid,final Long profeid,Date servicedate){
		BigDecimal ret = new BigDecimal(0).setScale(3,BigDecimal.ROUND_HALF_UP);
		final Date stDate;
		final Date edDate;
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(servicedate);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		stDate = calendar.getTime();

		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 999);
		edDate = calendar.getTime();
		
		Profession  profet = professionDao.findbyId(profeid);
		DataDict  dict = dataDictDao.findbyId(profet.getAccountmethod());
		final String sumColName;
		if(DictionaryUtils.DICT_CODE_ACCOUNTMETHOD_1.equals(dict.getDatacode())) 
			sumColName = "t.allworkhours";
		else
			sumColName = "t.allworkquantity";
		final String hql = "select sum(" + sumColName + ") from Cost t where t.companyid = :companyid and t.profeid = :profeid and t.servicedate >= :stDate and t.servicedate <= :edDate";
		
		Object obj = this.getHibernateTemplate().execute(new HibernateCallback() {
			
			@Override
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				Query query = session.createQuery(hql);
				query.setParameter("profeid", profeid);
				query.setParameter("companyid", companyid);
				query.setParameter("stDate", stDate);
				query.setParameter("edDate", edDate);
				return query.uniqueResult();
			}
		});
		if(obj != null)
			ret = new BigDecimal(obj.toString()).setScale(3,BigDecimal.ROUND_HALF_UP);
		
		return ret;
	}

	@Override
	public boolean check(BudgetNum budgetNum) {
		boolean flag = true;
		String queryString = "from Cost t where 1=1 and t.budgetnum =:budgetnum";
		try {
			Query query = this.getSession().createQuery(queryString);
			query.setParameter("budgetnum", budgetNum.getNumId());
			List<String> list = query.list();
			if(list == null || list.size() == 0) {
				flag = false;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}

	public IProfessionDao getProfessionDao() {
		return professionDao;
	}

	@Resource(name = "professionDao")
	public void setProfessionDao(IProfessionDao professionDao) {
		this.professionDao = professionDao;
	}

	public IDataDictDao getDataDictDao() {
		return dataDictDao;
	}

	@Resource(name = "dataDictDao")
	public void setDataDictDao(IDataDictDao dataDictDao) {
		this.dataDictDao = dataDictDao;
	}
}
